﻿using System;
using zhangmenren.Db;
using System.Net.Mime;
using Dapper;
using zhangmenren.Models;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using zhangmenren.Models.Enums;

namespace zhangmenren.Repository
{
    public class BannerRepository:BaseRepository
    {
       /// <summary>
       /// 添加banner
       /// </summary>
       /// <returns>The add.</returns>
       /// <param name="banner">Banner.</param>
        public int Add(BannerEntity banner){
            string sql = @"INSERT INTO Banner 
                            (ImgUrl
                            ,HrefUrl
                            ,State
                            ,Title
                            ,Describe,Type)
                        VALUES
                            (@ImgUrl
                            ,@HrefUrl
                            ,@State
                            ,@Title
                            ,@Describe,@Type)";
            return Execute(connection => connection.Execute(sql, banner));
        }

        /// <summary>
        /// 获取banner列表
        /// </summary>
        /// <returns>The select.</returns>
        public IEnumerable<BannerEntity> Select(){
            string sql = @"SELECT Id,ImgUrl,State,HrefUrl,Title,Describe,Type FROM Banner";
            return Execute(connection => connection.Query<BannerEntity>(sql));  //Items = items.Skip((pageIndex - 1) * pageSize).Take(pageSize)
		}

        /// <summary>
        /// 删除banner
        /// </summary>
        /// <returns>The del.</returns>
        /// <param name="id">banner标识</param>
        public int Del(int id){
            string sql = @"DELETE FROM Banner WHERE Id = @Id";
            return Execute(connection => connection.Execute(sql, new { Id = id}));
        }

        /// <summary>
        /// 编辑banner状态
        /// </summary>
        /// <returns>The del.</returns>
        /// <param name="id">banner标识</param>
        public int EditState(int id,bool state)
        {
            string sql = @"UPDATE Banner SET State = @State WHERE Id = @Id";
            return Execute(connection => connection.Execute(sql, new { Id = id ,State=state}));
        }

        /// <summary>
        /// 查询可用的banner
        /// </summary>
        /// <returns>The banner.</returns>
        public IEnumerable<BannerEntity> FindBanner(BannerType type)
        {
            string sql = @"SELECT Id,ImgUrl,State,HrefUrl,Title,Describe,Type FROM Banner WHERE State=1 AND Type=@Type";
            return Execute(connection => connection.Query<BannerEntity>(sql,new {Type=type }));
        }


    }
}
